package cn.sczc.jh.client.service.mapimage;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.sczc.jh.client.entity.MapImage;
import cn.sczc.jh.client.mapper.mapimage.MapImageMapper;
import cn.sczc.jh.client.vo.MapImageVo;

@Service
@Transactional(rollbackFor = Exception.class)
public class MapImageService extends ServiceImpl<MapImageMapper, MapImage> {
	@Autowired
	private MapImageMapper mapImageMapper;

	public MapImage addMapImage(Map<String, Object> params) {
		String filePath = params.get("filePath").toString();
		String fileName = params.get("fileName").toString();
		Integer type = Integer.valueOf(params.get("type").toString());
		Long tenantId = Long.valueOf(params.get("tenantId").toString());
		MapImage mapImage = new MapImage();
		mapImage.setOid(UUID.randomUUID().toString());
		mapImage.setTenantId(tenantId);
		mapImage.setImagePath(filePath);
		mapImage.setImageName(fileName);
		mapImage.setType(type);
		mapImage.setCreateTime(new Date());
		mapImageMapper.insert(mapImage);
		return mapImage;
	}

	public List<MapImage> getMapImages(Map<String, Object> params) {
		return mapImageMapper.getMapImages(params);
	}

	public int delById(String id) {
		return mapImageMapper.deleteById(id);
	}

	public List<MapImage> updateMapImage(List<MapImageVo> records) {
		List<MapImage> list = new ArrayList<MapImage>();
		if (records != null && records.size() > 0) {
			for (MapImageVo rec : records) {
				if (StringUtils.isBlank(rec.getImageId())) {
					continue;
				}
				String id = rec.getImageId();
				MapImage mapIamge = mapImageMapper.selectById(id);
				mapIamge.setImagePos(rec.getExtent());
				mapIamge.setRotation(rec.getRotation());
				mapImageMapper.updateById(mapIamge);
				list.add(mapIamge);
			}
		}
		return list;
	}

}
